﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Entities;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminCheckPoints : Form
    {
        
        private DisciplineListEntity discipline;
        
        // Constructor
        public frmAdminCheckPoints(int disciplineListId)
        {
            InitializeComponent();

            this.Icon = Properties.Resources.AtletIS64;
            this.discipline = Helpers.DisciplineListHelper.ConvertToEntity(DatabaseHelper.GetDisciplineListById(disciplineListId).Rows[0]); 
        }

        // Form Load
        private void frmCheckPoints_Load(object sender, EventArgs e)
        {
            DatabaseHelper.FillCheckPointsByDisciplineListId(this.aDbDataSet.CheckPoints, this.discipline.ID);
            this.lblTitle.Text += ": " + this.discipline.Name;
        }

        // Generate check-points
        private void btnGen_Click(object sender, EventArgs e)
        {
            int first, last, period, order;

            try
            {
                first = Convert.ToInt32(this.txtFirst.Text);
                last = Convert.ToInt32(this.txtLast.Text);
                period = Convert.ToInt32(this.txtPeriod.Text);
            }
            catch
            { 
                MessageBox.Show("Zadajte číselné údaje!", "Check-pointy", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            order = 1;
            while (first <= last)
            {
                using (CheckPointsTableAdapter taCheckPoints = new CheckPointsTableAdapter())
                {
                    taCheckPoints.Insert(this.discipline.ID, 
                        order, 
                        first.ToString() + " " + txtHeader.Text, 
                        3, 
                        "01", 
                        order, 
                        first.ToString(), 
                        0,
                        true, 
                        false, 
                        DateTime.Now);
                }
                
                first += period;
                order += 1;
            }

            // Reload dgv
            DatabaseHelper.FillCheckPointsByDisciplineListId(this.aDbDataSet.CheckPoints, this.discipline.ID);

            MessageBox.Show("Check-pointy boli vygenerované.", "Check-pointy", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            foreach (DataRow drCheckPoint in aDbDataSet.CheckPoints.Rows)
            {
                if (drCheckPoint.RowState != DataRowState.Deleted)
                {
                    drCheckPoint["DisciplineListId"] = discipline.ID;

                    if (drCheckPoint["Active"] == DBNull.Value)
                    {
                        drCheckPoint["Active"] = true;
                    }
                    
                    if (drCheckPoint["WebId"] == DBNull.Value)
                    {
                        drCheckPoint["WebId"] = 0;
                    }

                    if (drCheckPoint["Sync"] == DBNull.Value)
                    {
                        drCheckPoint["Sync"] = false;
                    }
                }
            }

            this.Validate();
            this.bsCheckPoints.EndEdit();
            this.taCheckPoints.Update(aDbDataSet.CheckPoints);

            // Reload dgv
            DatabaseHelper.FillCheckPointsByDisciplineListId(this.aDbDataSet.CheckPoints, discipline.ID);

            MessageBox.Show("Check-pointy boli uložené.", "Check-pointy", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        // Import
        private void tsbImport_Click(object sender, EventArgs e)
        {
            frmAdminCheckPointsImport frm = new frmAdminCheckPointsImport(discipline);
            frm.ShowDialog();

            DatabaseHelper.FillCheckPointsByDisciplineListId(this.aDbDataSet.CheckPoints, discipline.ID);
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


       
    }
}
